const mysql = require('../lib/mysql');
const oMysql = require('mysql');
const query = mysql.query;


let nssd =
    `CREATE TABLE IF NOT EXISTS nssd (
        id int(10) unsigned NOT NULL AUTO_INCREMENT,
        d_id varchar(255) NOT NULL DEFAULT '' COMMENT '文章id',
        title varchar(255) NOT NULL DEFAULT '' COMMENT '文章标题',
        abstract varchar(3000) NOT NULL DEFAULT '' COMMENT '摘要',
        author varchar(255) NOT NULL DEFAULT '' COMMENT '作者',
        hdclassname varchar(255) NOT NULL DEFAULT '' COMMENT '分类',
        c_title varchar(255) NOT NULL DEFAULT '' COMMENT '参考文献标题',
        c_author varchar(255) NOT NULL DEFAULT '' COMMENT '参考文献作者',
        c_journal varchar(255) NOT NULL DEFAULT '' COMMENT '出版社',
        c_date int(10) unsigned NOT NULL DEFAULT '0' COMMENT '出版时间',
        institution varchar(255) NOT NULL DEFAULT '' COMMENT '作者单位',
        date varchar(255) NOT NULL DEFAULT '' COMMENT '出版日期',
        journal varchar(255) NOT NULL DEFAULT '' COMMENT '期刊',
        key_word varchar(255) NOT NULL DEFAULT '' COMMENT '关键词',
        fund varchar(255) NOT NULL DEFAULT '' COMMENT '基金项目',
        created_at int(10) unsigned NOT NULL DEFAULT '0' COMMENT '录入时间',
        updated_at int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
        PRIMARY KEY (id),
        KEY d_id (d_id)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;`;


//插入数据
let insertData1 = (value) => {
    let _sql = "insert into nssd set d_id=?,title=?,author=?,book=?,publish=?,publish_time=?,created_at=?,updated_at=?;"
    return query(_sql, value)
}

//插入数据
let insertData = (value) => {
    let dataBaseInser = [];
    let params = [];
    for (kk in value) {
        dataBaseInser.push(`${kk}=${oMysql.escape(value[kk])}`)
    }
    let _sql = `insert into nssd set ${dataBaseInser.join(',')};`;
    return query(_sql, params)
}
// 删除用户
let deleteUserData = (name) => {
    let _sql = `delete from users where name="${name}";`
    return query(_sql)
}

// 查找文章
let findNssdData = async function (d_id) {
    let _sql = `select * from nssd where d_id="${d_id}";`
    return await query(_sql);
}

let createTable = (sql) => {
    return query(sql, [])
}

// 建表
createTable(nssd);

module.exports = {
    createTable,
    findNssdData,
    insertData
}